﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CodeGenerator.Core;
using CodeGenerator.Common;

namespace CodeGenerator.UI.BuildHControl
{
    public partial class UcListControl : UserControl
    {
        public UcListControl()
        {
            InitializeComponent();
        }

        private void UcListControl_Load(object sender, EventArgs e)
        {
            ServerInfo server = Constant.CurrentServerInfo;
            if (server != null && !string.IsNullOrEmpty(Constant.DatabaseName))
            {
                IDatabase database = DBFactory.Database(server.DBType);

                string dbName = Constant.DatabaseName;
                string tableName = Constant.TableName;
                List<ColumnInfo> columnInfoList = database.GetColumnInfoList(server, dbName, tableName);

                List<CreateListCode> list = new List<CreateListCode>();

                foreach (var column in columnInfoList)
                {
                    CreateListCode model = new CreateListCode();
                    model.No = column.Id;
                    model.Lable = column.Comments;
                    model.Field = column.Name;
                    model.Hidden = false;
                    model.Sortable = true;
                    model.Align = "left";
                    model.Width = 100;
                    model.Format = "字符串";
                    if (column.IsDateTime)
                    {
                        model.Format = "日期";
                    }
                    else if (column.IsBoolean)
                    {
                        model.Format = "布尔";
                    }
                    model.SortCode = Convert.ToInt32(column.Id);
                    model.Show = true;

                    list.Add(model);
                }

                dataGridView1.DataSource = list;
                DataGridViewEx.AutoSizeColumn(dataGridView1);
            }

           
        }
    
        public List<CreateListCode> GetListCode()
        {
            List<CreateListCode> list = (List<CreateListCode>)dataGridView1.DataSource;

            return list.OrderBy(t => t.SortCode).ToList();
        }

        public Dictionary<string,bool> GetBtnInfo()
        {
            Dictionary<string, bool> dict = new Dictionary<string, bool>();
            dict.Add("Refresh", chkAdd.Checked);
            dict.Add("Add", chkAdd.Checked);
            dict.Add("Edit", chkEdit.Checked);
            dict.Add("Detail", chkDetail.Checked);
            dict.Add("Delete", chkDelete.Checked);
            dict.Add("Close", chkQuit.Checked);
            
            return dict;
        }
    }
}
